package me.boot.web.common.template.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import me.boot.db.mybatis.template.SuperService;
import me.boot.db.mybatis.template.entity.Entity;
import me.boot.web.common.template.model.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;

public interface UpdateController<
        ID extends Serializable,
        PO extends Entity<ID>,
        S extends SuperService<ID, PO>,
        DTO>
        extends BaseController<ID, PO, S> {

    @ApiOperation(value = "单体修改")
    @ApiOperationSupport(order = 21)
    @PostMapping(value = "/update")
    default Result<Boolean> updateById(@RequestBody @Validated DTO updateDTO) {
        PO po = fromUpdateDTO(updateDTO);
        return Result.newSuccess(getService().updateById(po));
    }


    @ApiOperation(value = "全量修改所有字段", notes = "修改所有字段，没有传递的字段会被置空")
    @ApiOperationSupport(order = 22)
    @PostMapping("/update/all")
    default Result<Boolean> updateAllById(@RequestBody @Validated DTO updateDTO) {
        PO po = fromUpdateDTO(updateDTO);
        return Result.newSuccess(getService().updateAllById(po));
    }

    @ApiOperation(value = "批量修改")
    @ApiOperationSupport(order = 23)
    @PostMapping(value = "/update/batch")
    default Result<Boolean> updateBatchById(@RequestBody @Validated List<DTO> updateDTOS) {
        List<PO> list = Lists.newArrayList();
        updateDTOS.forEach(dto -> list.add(fromUpdateDTO(dto)));
        return Result.newSuccess(getService().updateBatchById(list));
    }


    default PO fromUpdateDTO(DTO updateDTO) {
        return dtoToPo(updateDTO);
    }
}
